home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
016a
/
frbts_20.zip
/
FILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-04-28
|
11KB
|
428 lines
/****************************************************************************/
/* */
/* File.c Version 2.0 By Craig Derouen */
/* File i/o processsing module for Frobot.c */
/* */
/* */
/****************************************************************************/
#include "frobot.h"
#include <share.h>
#include <time.h>
#include "globals.h"
/* Parse out the parameter file */
int getparms(char *ctlfile)
{
FILE *infile;
char *p1,*p2;
int checkcount = 0;
if ((infile = _fsopen(ctlfile,"rt",SH_DENYNO)) == NULL)
return(TRUE); /* Say there's an error */
fgets(temp,81,infile);
while (!feof(infile)) {
if (*temp) { /* Parse out token string */
p1 = strtok(temp," \n\t");
p2 = strtok(NULL," \n\t;");
if (*p1 == ';') { /* Just ignore comment lines */
fgets(temp,81,infile);
continue;
}
if (strcmpi(p1,"node") == 0) {
if (p2) {
if (!parsenet(p2,&Zone,&Net,&Node))
checkcount++;
}
}
else if (strcmpi(p1,"outbound") == 0) {
if (p2) {
striptail(p2);
strcpy(Outbdir,p2);
checkcount++;
}
}
else if (strcmpi(p1,"inbound") == 0) {
if (p2){
striptail(p2);
strcpy(Inbdir,p2);
checkcount++;
}
}
else if (strcmpi(p1,"netmail") == 0) {
if (p2) {
striptail(p2);
strcpy(Netmdir,p2);
checkcount++;
}
}
else if (strcmpi(p1,"scriptloc") == 0) {
if (p2) {
strcpy(ScriptFile,p2);
checkcount++;
}
}
else if (strcmpi(p1,"logfile") == 0) {
if (p2) strcpy(LogFile,p2);
}
else if (strcmpi(p1,"display") == 0) {
if (p2){
if (strcmpi(p2,"Yes") == 0)
ShowActivity = TRUE;
}
}
else if (strcmpi(p1,"selfmail") == 0) {
if (p2){
if (strcmpi(p2,"Yes") == 0)
SelfMail = TRUE;
}
}
else if (strcmpi(p1,"sysopmail") == 0) {
if (p2){
if (strcmpi(p2,"Yes") == 0)
SysopMail = TRUE;
}
}
else if (strcmpi(p1,"bells") == 0) {
if (p2){
if (strcmpi(p2,"Yes") == 0)
Silent = FALSE;
else Silent = TRUE;
}
}
}
fgets(temp,81,infile);
}
fclose(infile);
if (checkcount < 5)
return(2); /* Not all the necessary parameters were found */
return(FALSE);
}
/* Process the script file */
int doscript(void)
{
FILE *sfile;
FILE *bfile;
char *linebuff,*p;
char backup[_MAX_PATH];
int code = FALSE;
/* First thing is to create a backup file for the script */
createbackup(ScriptFile,backup);
if ((bfile = _fsopen(backup,"wt",SH_DENYRW)) == NULL)
aborterror(BADFILE,NULL);
if ((sfile = _fsopen(ScriptFile,"rt",SH_DENYNO)) == NULL)
aborterror(BADFILE,NULL);
time(&now);
ltime = localtime(&now);
p = strtok(asctime(ltime)," ");
strcpy(datestr,p);
strcat(datestr,", ");
strcat(datestr,strtok(NULL," "));
strcat(datestr," ");
strcat(datestr,strtok(NULL," "));
strcat(datestr," ");
strtok(NULL," ");
strcat(datestr,strtok(NULL," \n"));
linebuff = (char *) malloc(MAXLINE_SIZE+1);
if (linebuff == NULL)
aborterror(NOMEM,NULL);
linenum = 0;
FidoMsgNote[0] = 0;
fgets(linebuff,MAXLINE_SIZE,sfile);
while (!feof(sfile)) {
linenum++;
if (*linebuff) { /* Parse out token string */
if (*linebuff != ';' && *linebuff != '\n')
procline(linebuff);
}
if (*linebuff != '\n') /* Remove blank lines! */
fputs(linebuff,bfile);
fgets(linebuff,MAXLINE_SIZE,sfile);
}
free(linebuff);
fclose(bfile);
fclose(sfile);
if(remove(ScriptFile) == 0)
rename(backup,ScriptFile);
else {
warble();
printf("Error changing script file\n");
code = -1;
remove(backup);
}
return(code);
}
/* Process valid line from script file */
void procline(char *line)
{
char *workline;
char *p1,*p2,*fname;
int x;
blanktrim(line);
strcat(line,"\n");
if (*line == '\n')
return; /* It's a blank line */
/* Create a 2nd copy of the script line for munging with */
workline = (char *) malloc(strlen(line) + 10);
if (workline == NULL)
aborterror(NOMEM,NULL);
strcpy(workline,line);
if (ShowActivity)
printf("Processing line # %d\n",linenum);
p1 = strtok(workline," ;\t\n"); /* Initial token finds out send or rcv */
strlwr(p1); /* Makes testing easier */
p2 = strtok(NULL,"\n"); /* get rest of line */
if (!validate(p2,line)) { /* Check date/time */
p2 = p1;
p2 += strlen(p1) + 1;
blanktrim(p2);
fname = strtok(p2," ;\t\n"); /* Bump to node */
for (x=0; x < 4; x++)
p2 = strtok(NULL," ;\t\n"); /* Bump rest */
/* p2 now has fidonet mode */
FidoNetMode = (char) toupper(*p2);
/* Build flowfile and req filenames */
strcpy(FloFile,Outbdir);
strcpy(ReqFile,Outbdir);
sprintf(temp,"\\%04x%04x.",DestNet,DestNode);
strcat(ReqFile,temp);
strcat(ReqFile,"REQ");
if (FidoNetMode == 'N')
strcat(temp,"FLO");
else if (FidoNetMode == 'N')
strcat(temp,"DLO");
else {
x = strlen(temp);
temp[x] = FidoNetMode;
temp[x+1] = 0;
strcat(temp,"LO");
}
strcat(FloFile,temp);
p2 = strtok(NULL," =;\n"); /* Bump to rest */
if (p2) blanktrim(p2); /* Bump to count */
if (*p1 == 's') /* File send */
file_send(p1,line,fname);
else if (*p1 == 'r') { /* file request */
if (p2) {
FileCount = atoi(p2);
p2 = strtok(NULL," ;\n"); /* See if there's a xfercount */
if (p2) XferTries = atoi(p2);
else XferTries = -1;
file_request(line,fname);
}
else { /* No count, try it once! */
FileCount = 1;
XferTries = -1;
/* Fix up line */
x = strlen(line);
line[x-1]=0; /* Strip linefeed */
strcat(line," 1\n");
file_request(line,fname);
}
}
/* Anything else we just ignore and comment out! */
else commentline(line," Invalid command,ignored!");
}
free(workline);
}
/* Check dirpath and remove tail dir sep if its there */
void striptail(char *p)
{
int x;
x = strlen(p) - 1;
if (p[x] == '\\')
p[x] = 0;
}
/* Comment out the current line and append possible string */
void commentline(char *line,char *str)
{
char *p1;
strinsert(";",line,0); /* Force it to be ignored next time */
if (str != NULL) {
p1 = strchr(line,'\n');
if(p1 != NULL)
*p1 = 0; /* Remove linefeed so we can append */
strcat(line," <-");
strcat(line,str);
if (!strchr(str,'\n'))
strcat(line,"\n");
}
}
/* Test for valid date/time
return -1 if error, 0 if okay, 1 if don't process */
int validate(char *input,char *line)
{
char *datep;
char *timep;
char *savep;
char *p1;
int x,code;
/* input -> token line, line -> original line */
p1 = (char *) malloc(strlen(input) + 1);
if (p1 == NULL)
aborterror(NOMEM,NULL);
strcpy(p1,input);
if(setdest(p1,line)) { /* setup the global node destination,file */
free(p1);
return(-1); /* An error occured */
}
savep = strtok(p1," "); /* Bump past filename */
savep = strtok(NULL," "); /* Bump past node */
savep = strtok(NULL,"\n"); /* Bump past node */
datep = strtok(savep," "); /* Get date string */
timep = strtok(NULL," "); /* Get time string */
if (datep) {
x = datecheck(datep);
switch (x) {
case -2: /* Invalid date string */
commentline(line,"Invalid date!");
code = -1;
break;
case -1: /* Older so process now! */
code = 0;
break;
case 0: /* Today, so check time first */
x = timecheck(timep);
if (x == -2) {
commentline(line,"Invalid time!");
code = -1;
}
else if (x == -1 || x == 0) /* Ok to process now */
code = 0;
else code = 1; /* Not current yet */
break;
default: /* Well don't touch anything */
code = 1;
break;
}
}
else {
commentline(line,"Invalid date!");
code = -1;
}
free(p1);
return(code);
}
/* Check to see if filename is inside infile (text mode)
return TRUE if so */
int filescan(FILE *infile,char *fname)
{
static char fileline[100];
char *p;
rewind(infile);
while(fgets(fileline,99,infile)) {
p = strtok(fileline," \n"); /* Strip out file */
if (*p == '#' || *p == '^') /* Special kludges for flo files */
p++;
if(strcmpi(p,fname) == 0)
return(TRUE);
}
return(FALSE);
}
/* Create a backup file with ext ".$$$" */
void createbackup(char *input,char *output)
{
int x;
char *p;
strcpy(output,input);
x = strlen(output) - 1;
p = &output[x];
while (*p != '.' && *p != '\\')
p--;
switch (*p) {
case '\\': /* Humm, a fullpath/filename with no extension */
p++;
strcat(p,".$$$");
break;
case '.':
p++;
strcpy(p,"$$$");
break;
}
}
void logit(char *strng,char flag)
{
FILE *log;
char ttemp[40];
char *ttemp1;
char *month;
char *day;
char *tm;
if (LogFile[0] == 0)
return;
ttemp1 = (char *) malloc(101);
if (ttemp1 == NULL) {
LogFile[0] = 0;
aborterror(NOMEM,NULL);
}
strcpy(ttemp, timestring());
strtok(ttemp," "); /* Strip Day of week */
month = strtok(NULL," ");
day = strtok(NULL," ");
tm = strtok(NULL," ");
sprintf(ttemp1,"%c %s %s %s Frobot %s\n",flag,day,month,tm,strng);
if ((log = _fsopen(LogFile,"a",SH_DENYWR)) == NULL) {
printf("\a\aError opening log file!\n");
LogFile[0] = 0;
free(ttemp1);
return;
}
fputs(ttemp1,log);
free(ttemp1);
fclose(log);
}
char *timestring(void)
{
time_t aclock;
struct tm *newtime;
time(&aclock);
newtime = localtime(&aclock);
return(asctime(newtime));
}